package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.CategoryDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CategoryDAO {
	protected static final String TABLE_NAME = "Category";
	protected static final String ID = "rowid";
	protected static final String CTG_ID = "ctg_id";
	protected static final String CTG_NM = "ctg_nm";
	protected static final String CTG_TYPE = "ctg_type";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ CTG_ID + " text, " + CTG_NM + " text);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public CategoryDAO(Context context) {
		this.context = context;
	}

	public ArrayList<CategoryDTO> getAll() {
		ArrayList<CategoryDTO> categoryDTOs = new ArrayList<CategoryDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				CategoryDTO categoryDTO = this.cursorToCategoryDTO(cursor);
				categoryDTOs.add(categoryDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return categoryDTOs;
	}

	public CategoryDTO get(int id) {
		CategoryDTO categoryDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, CTG_ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			categoryDTO = this.cursorToCategoryDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return categoryDTO;
	}

	public void insert(CategoryDTO categoryDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this
				.categoryDTOToContentValues(categoryDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(CategoryDTO categoryDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = categoryDTO.getId();
		categoryDTO.setId(-1);
		ContentValues contentValues = this
				.categoryDTOToContentValues(categoryDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private CategoryDTO cursorToCategoryDTO(Cursor cursor) {
		CategoryDTO categoryDTO = new CategoryDTO();
		int id = cursor.getInt(cursor.getColumnIndex(ID));
		categoryDTO.setId(id);
		String ctgId = cursor.getString(cursor.getColumnIndex(CTG_ID));
		categoryDTO.setCtgId(ctgId);
		String ctgNm = cursor.getString(cursor.getColumnIndex(CTG_NM));
		categoryDTO.setCtgNm(ctgNm);
		String ctgType = cursor.getString(cursor.getColumnIndex(CTG_TYPE));
		categoryDTO.setCtgType(ctgType);
		return categoryDTO;
	}

	private ContentValues categoryDTOToContentValues(CategoryDTO categoryDTO) {
		ContentValues contentValues = new ContentValues();
		int id = categoryDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String ctgId = categoryDTO.getCtgId();
		if (ctgId != null) {
			contentValues.put(CTG_ID, ctgId);
		}
		String ctgNm = categoryDTO.getCtgNm();
		if (ctgNm != null) {
			contentValues.put(CTG_NM, ctgNm);
		}
		String ctgType = categoryDTO.getCtgType();
		if (ctgType != null) {
			contentValues.put(CTG_TYPE, ctgType);
		}
		return contentValues;
	}
}